package cn.sinata.wanglin.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Contact;
import cn.sinata.wanglin.utils.ImageUtil;

/**
 *
 * Created by liaoxiang on 16/12/9.
 */

public class ContactAdapter extends HFRecyclerAdapter<Contact> implements Filterable{

    private static final int TYPE_GROUP_TITLE = 10;
    private HashMap<String ,Integer> letters = new HashMap<>();
    private String housingId;

    private ContactFilter mFilter;
    private ArrayList<Contact> mUnfilteredData;

    public ContactAdapter(List<Contact> mData) {
        super(mData, R.layout.item_contact_layout);
        housingId = SharedPreferencesUtils.getString(Constant.SP_HOUSING_ID);
    }

    public void setLetters(HashMap<String,Integer> letters) {
        this.letters = letters;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_GROUP_TITLE) {
            LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
            View view = mInflater.inflate(R.layout.item_contact_group_layout, parent, false);
            return new ViewHolder(view);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBind(int position, Contact contact, ViewHolder holder) {
        int type = getItemViewType(hasHeader()?position+1:position);
        if (type == TYPE_GROUP_TITLE) {
            TextView tv_head = holder.bind(R.id.tv_head);
            Utils.systemErr(contact.getFirstChar());
            tv_head.setText(contact.getFirstChar());
        }
        holder.setText(R.id.tv_name,contact.getName());
        SimpleDraweeView headView = holder.bind(R.id.headView);
        headView.setImageURI(ImageUtil.parseImageUri(contact.getHeadUrl()));
        //是否同小区
//        boolean isSame = TextUtils.equals(contact.getHousingID(),housingId);
        int isSame = contact.getIdentical();
        holder.bind(R.id.tv_same_village).setVisibility(isSame==1?View.VISIBLE:View.GONE);
    }

    @Override
    public int getItemViewType(int position) {
        int type = super.getItemViewType(position);
        if (type == TYPE_NORMAL) {
            int p = position;
            if (hasHeader()) {
                p = position-1;
            }
            Contact contact = mData.get(p);
            String firstChar = contact.getFirstChar();
            if (letters.containsKey(firstChar) && letters.get(firstChar) == p) {
                return TYPE_GROUP_TITLE;
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ContactFilter();
        }
        return mFilter;
    }

    private class ContactFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList<>(mData);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<Contact> list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<Contact> unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList<Contact> newValues = new ArrayList<>(count);

                for (int i = 0; i < count; i++) {
                    Contact h = unfilteredValues.get(i);
                    if (h != null) {
                        String word = h.getName();
                        if (word.toLowerCase().contains(prefixString)) {
                            newValues.add(h);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mData = (List<Contact>) results.values;
            notifyDataSetChanged();
        }
    }
}
